package redis.inaction.xx.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.inaction.xx.cache.UserStatusRedisCache;
import redis.inaction.xx.entity.UserStatus;
import redis.inaction.xx.repository.UserStatusRepository;

import java.util.Date;

@Service
@Slf4j
public class UserStatusService {

    @Autowired
    private UserStatusRedisCache userModelRedisCache;

    @Autowired
    private UserStatusRepository userStatusRepository;

    public Long getUserStatus(String userId){
        return userModelRedisCache.getUserStatus(userId);
    }

    public boolean setUserStatus(String uid, long newStatus){
        //transaction
        userStatusRepository.save(UserStatus.builder()
                .id(new Long(uid))
                .status(newStatus)
                .lastUpdate(new Date())
                .build());

        userModelRedisCache.deleteCache(uid);
        return true;
    }
}
